local Talented = Talented
local COLORS = {
	[-2] = { 1, 0, 0}, -- red
	[-1] = { 1, .5, 0}, -- orange
	[0] = { 1, 1, 0 }, -- yellow
	[1] = { 1, 1, 1 }, -- white
	[2] = { 0, 1, 0 }, -- green
}

Talented.advices = {}

local db = Talented:AcquireDBNamespace("Advices")
Talented:RegisterDefaults("Advices", "profile", {
	always_show_positive = false,
	always_show_negative = false,
	selections = {},
})

local build_valid_selection
do
	local table, current
	build_valid_selection = function(class)
		if not class then return end
		if class ~= current then
			table = {}
			for name in pairs(Talented.advices[class]) do
				table[name] = name
			end
		end
		return table
	end
end


Talented.options.args.options.args.advices = {
	name = "Advices",
	desc = "Provide advice to construct a spec",
	type = "group",
	order = 800,
	args = {
		always_show_positive = {
			name = "Always show positive advice",
			desc = "Always show the positive advice for you current selection, even if you can't invest points in the talents.",
			type = "toggle",
			get = function () return db.profile.always_show_positive end,
			set = function (v) db.profile.always_show_positive = v Talented:UpdateView() end,
			order = 1,
		},
		always_show_negative = {
			name = "Always show negative advice",
			desc = "Always show the negative advice for you current selection, even if you haven't invested a point in the talent.",
			type = "toggle",
			get = function () return db.profile.always_show_negative end,
			set = function (v) db.profile.always_show_negative = v Talented:UpdateView() end,
			order = 2,
		},
		selection = {
			name = "Advice selection",
			desc = "Select the type of advice you want for this class.",
			usage = "<type>",
			type = "text",
			disabled = function () return not Talented.template or not Talented.template.class end,
			get = function () return db.profile.selections[Talented.template.class] end,
			set = function (v) db.profile.selections[Talented.template.class] = v end,
			validate = function () return build_valid_selection(Talented.template.class) or {} end,
		},
	},
}

local function IsAdviceVisible(template, tab, index, value)
	if value < 0 then
		if db.profile.always_show_negative then return true end
		local state = Talented:GetTalentState(template, tab, index)
		if state == "full" or state == "available" then return true end
	else
		if db.profile.always_show_negative then return true end
		local state = Talented:GetTalentState(template, tab, index)
		if state ~= "full" and state ~= "unavailable" then return true end
	end
end

local uv = Talented.UpdateView
function Talented:UpdateView()
	uv(self)
	self:ClearAllShines()
	
	local template = self.template
	local class = template.class
	local selection = db.profile.selections[class]
	local advices = selection and self.mode ~= "apply" and self.advices[class][selection]
	if not advices then return end
	
	local info = self:GetTalentInfo(class)
	
	for _, advice in ipairs(advices) do
		local tab, index, value = unpack(advice, 1, 3)
		if IsAdviceVisible(template, tab, index, value) then
			self:MakeShine(info[tab].talents[index].button, unpack(COLORS[value]))
		end
	end
end

local sti = Talented.SetTooltipInfo
function Talented:SetTooltipInfo(frame, tab, index)
	sti(self, frame, tab, index)
	
	local template = self.template
	local class = template.class
	local selection = db.profile.selections[class]
	local advices = selection and self.mode ~= "apply" and self.advices[class][selection]
	if not advices then return end
	
	for _, advice in ipairs(advices) do
		local atab, aindex, value, desc = unpack(advice)
		if atab == tab and aindex == index and IsAdviceVisible(template, tab, index, value) then
			local r, g, b =  unpack(COLORS[value])
			GameTooltip:AddLine("Advice: "..desc, r, g, b, true)
			GameTooltip:Show()
		end
	end
end
